import java.util.*;

/**
 * @author LKQ
 * @date 2022/5/21 12:06
 * @description 层序遍历 DFS
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    int n = 10000;
    int depth = 0;
    int[] levelSum = new int[n];

    public void inorder(TreeNode node, int level) {
        if (node != null) {
            inorder(node.left, level + 1);
            levelSum[level] += node.val;
            depth = Math.max(depth, level);
            inorder(node.right, level + 1);
        }
    }

    public int maxLevelSum(TreeNode root) {
        inorder(root, 1);
        int maxIdx = 0;
        for (int i = 1; i <= depth; ++i) {
            maxIdx = levelSum[i] > levelSum[maxIdx] ? i : maxIdx;
        }
        return maxIdx;
    }

}
